﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Photon.SocketServer;
using TaiDouCommon;
using TaiDouCommon.Tools;
using TaiDouPhotonServer;
using TaiDouPhotonServer.DB.Manager;
using TaiDouCommon.Model;

namespace TaiDouPhotonServer.Handlers
{
    class RoleHandler:HandlerBase
    {
        private RoleManager manager;
        public RoleHandler()
        {
            manager = new RoleManager();
        }
        public override OperationCode OpCode
        {
            get
            {
                return OperationCode.Role;
            }
        }
        public override OperationResponse OnHandleMessage(OperationRequest request, ClientPeer peer)
        {
            OperationResponse response = new OperationResponse(request.OperationCode);
            SubCode subCode = ParameterTool.GetParameters<SubCode>(request.Parameters, ParameterCode.SubCode, false);
            Dictionary<byte, object> parameters = new Dictionary<byte, object>();
            parameters.Add((byte)ParameterCode.SubCode, subCode);
            response.Parameters = parameters;
            switch (subCode)
            {
                case SubCode.GetRole://查询角色列表
                    List<Role> roleList = manager.GetRoleListByUser(peer.loginUser);
                    foreach (var role1 in roleList) {
                        role1.user = null;
                    }
                    ParameterTool.AddParameters<List<Role>>(parameters, ParameterCode.RoleList, roleList, true);                  
                    response.ReturnCode = (short)ReturnCode.Succ;
                break;
                case SubCode.AddRole://添加角色
                    Role role = ParameterTool.GetParameters<Role>(request.Parameters, ParameterCode.Role, true);
                    role.user = peer.loginUser;
                    manager.AddRole(role);
                    //清空user
                    role.user = null;
                    ParameterTool.AddParameters<Role>(parameters, ParameterCode.Role, role, true);
                    response.ReturnCode = (short)ReturnCode.Succ;
                break;
                case SubCode.SelectRole://选择角色
                    Role role2 = ParameterTool.GetParameters<Role>(request.Parameters, ParameterCode.Role, true);
                    peer.loginRole = role2;
                    //ParameterTool.AddParameters<Role>(parameters, ParameterCode.Role, role2, true);
                    response.ReturnCode = (short)ReturnCode.Succ;
                break;
            }

            return response;

        }
    }
}
